#include "stdafx.h"
#include "Keylistner.h"
#include "AddNodeDialog.h"
#include "iostream"
#include "SaveTo.h"
#include "ReadFrom.h"
#include "Paste.h"
#include "GetFile.h"




using namespace RedDot;
using namespace System::Runtime::InteropServices;
Keylistner::Keylistner(System::Windows::Forms::KeyEventArgs^ e)
{
	//E=e->KeyData;
	tField=false;
	posH = 0;
	posV = 0;
	nazwa = "";
	

}

Keylistner::Keylistner(Root *tr)
{
	tree=tr;
	wezel=(Node*)tr;
	shift=false;
	control=false;
	tField=false;
	modified = false;
	posH = 0;
	posV = 0;
	podswietlony=NULL; 
	nazwa = "";

}
//Bury 10.04.07 od
//funkcja edytujaca nody - info w Keylistner.h
void Keylistner::editNode(Node* wezel)
{
	AddNodeDialog ^dialog2 = gcnew AddNodeDialog(2,tree,wezel,wezel->getNodeName());//gdy pierwszym argumentem jest "2" to edycja
	dialog2->ShowDialog();
	modified = true;

}
//Bury 10.04.07 do

void Keylistner::KeyDown(System::Windows::Forms::KeyEventArgs^ e)
{
	//System::Windows::Forms::Keys E;
	E=e->KeyCode;
/*4.05 Pawel*/	
	if(!e->Control&&!e->Alt&&!e->Shift)
	switch(E){
		case (Windows::Forms::Keys::Insert):
//			Node *n1 = new Node("",System::Drawing::Color::Black.ToArgb(),true);
			if(true)
			{
				Node *tmp=new Node("",System::Drawing::Color::Black.ToArgb(),wezel->strona);
				wezel->addChild("",tmp);
			
				tField=true;
				if(wezel->strona)
					tF_X = tmp->getX();
				else 
					tF_X = tmp->getX()-100;
				tF_Y = tmp->getY();
				wezel->focus=false;
				wezel=tmp;
				edited = tmp;
				modified = true;

			}

			break;
			
			/*{
				AddNodeDialog ^ dialog = gcnew AddNodeDialog(0,tree,wezel);
//				dialog->currentWorksheet = this->currentWorksheet;
				dialog->ShowDialog();
//				pozycja=currentWorksheet->getCount()-1;	
				wezel=wezel->last;
			}
			break;*/
		case (Windows::Forms::Keys::V):
		case (Windows::Forms::Keys::Space):
			{
				if(wezel->first!=NULL)	
				  wezel->visibility();
				modified = true;
			}
			break;
		case (Windows::Forms::Keys::R):
			{
				if(wezel->link==true){
				GetFile(wezel->NodeName.c_str());
				Node *ble=new Node("nazwa",0,true);
				if(wezel->first)
					wezel->first->remove();
				ble=ReadFrom("C:/Documents and Settings/RedDot/net.rdt",NULL);
				ble->setInvisible();
				Paste(ble,wezel);
				ble->setVisible();
				wezel=ble;
				wezel->link=false;
				if(wezel->previous) wezel->previous->focus=false;
				wezel->focus=1;
				modified = true;
				}
			}
			break;
		//Bury 10.04.07 od
		case (Windows::Forms::Keys::F2)://edycja nodow za pomoca klawiatury
			{
					editNode(wezel);
					modified = true;
			}
			break;
        //Bury 10.04.07 do
		case (Windows::Forms::Keys::Q): {
			//	wezel->changeColour(Drawing::Color::Blue.ToArgb());
				break;
										   }
		case (Windows::Forms::Keys::Enter):
			{  
				Node *n1 = new Node("",System::Drawing::Color::Black.ToArgb(),wezel->strona);
			modified = true;
			
			
			if(wezel->father) {
			   wezel->addBrother("",n1);
			   if(wezel->strona)
					tF_X = n1->getX();
			   else
				   tF_X = n1->getX()-100;
			   tF_Y = n1->getY();
			   edited=n1;
			   tField=true;
			   wezel=n1;
			}	else {
				/*
				tF_X = wezel->last->getX();
			   tF_Y = wezel->last->getY();
			   edited=wezel->last;*/
				this->editNode(wezel);
			}	
				
			
				break;
			}	
		case (Windows::Forms::Keys::Up):
				wezel=wezel->inUp();
			
			break;
		case (Windows::Forms::Keys::Down):
				wezel=wezel->inDown();
			break;
		case (Windows::Forms::Keys::Right):
			{
				if(wezel->strona)
				{
					if(wezel->father)
						wezel=wezel->inRight();
					else
					{
						wezel->focus=false;
						wezel=wezel->first->inRight();
					}
				}
				else
					wezel=wezel->inLeft();
			break;
			}
		case (Windows::Forms::Keys::Left):
			{
				if(wezel->strona)
				{
					if(wezel->father)
						wezel=wezel->inLeft();
					else
					{
						wezel->focus=false;
						wezel=wezel->last->inRight();
					}
				}
				else
					wezel=wezel->inRight();

			break;
			}
//Pawel 13.03.07 od
		case (Windows::Forms::Keys::Delete):
		/*		Node *tmp=wezel;
				if(wezel->father)
				{
					wezel=wezel->remove();
					delete tmp;
				}*/

			break;
//Pawel 13.03.07 do

	}
	if(!e->Control&&e->Alt&&!e->Shift)
	{
		switch(E)
		{
			case (Windows::Forms::Keys::Up):
				wezel->move(0,3);
				modified = true;
				break;
			case (Windows::Forms::Keys::Down):
				wezel->move(0,-3);
				modified = true;
				break;
			case (Windows::Forms::Keys::Right):
				wezel->move(3,0);
				modified = true;
				break;
			case (Windows::Forms::Keys::Left):
				wezel->move(-3,0);
				modified = true;
				break;
			case (Windows::Forms::Keys::Insert):
				modified = true;
				if(!wezel->father)
				{
					Node *tmp=new Node("",System::Drawing::Color::Black.ToArgb(),!wezel->strona);
					wezel->addChild("",tmp);
			
					tField=true;
					tF_X = tmp->getX()-100;
					tF_Y = tmp->getY();
					wezel->focus=false;
					wezel=tmp;
					edited = tmp;
				}
				break;
		}
	}

	if(e->Control&&!e->Alt&&!e->Shift)
		switch(E){
			case (Windows::Forms::Keys::C):
//					SaveTo("C:/Documents and Settings/RedDot/tmp.rdt",wezel,NULL);
				break;
			case (Windows::Forms::Keys::V):
				{
/*					Node *ble=new Node("nazwa",0,true);
					ble=ReadFrom("C:/Documents and Settings/RedDot/tmp.rdt",NULL);
					ble->setInvisible();
//					ble->mirror();
					Paste(ble,wezel);
					ble->setVisible();
					wezel=ble;
					if(wezel->previous) wezel->previous->focus=false;
					wezel->focus=1;
*/				break;
				}
			case (Windows::Forms::Keys::Up):
					wezel->nodeUp();
					modified = true;
				break;
			case (Windows::Forms::Keys::Down):
					wezel->nodeDown();
					modified = true;
				break;
			case (Windows::Forms::Keys::Left):
				{
					modified = true;
				if(wezel->strona&&wezel->father&&wezel->father->father&&wezel->father->father->father)
					wezel->nodeLeft();
				break;
				}
			case (Windows::Forms::Keys::Right):
				modified = true;
				if(!wezel->strona&&wezel->father&&wezel->father->father&&wezel->father->father->father)
					wezel->nodeLeft();
				break;
			case (Windows::Forms::Keys::Enter):
			{  
					modified = true;
					AddNodeDialog ^dialog1 = gcnew AddNodeDialog(1,tree,wezel);
					dialog1->ShowDialog();
					if(wezel)
						if(wezel->father&&wezel->next) wezel=wezel->next;
					else 
					  if(!wezel->father)	
						wezel=wezel->last;
					  else 
							  wezel = wezel->father->last;

						
				
			break;
			}	
			case (Windows::Forms::Keys::Insert): 
             {
				AddNodeDialog ^ dialog = gcnew AddNodeDialog(0,tree,wezel);
				dialog->ShowDialog();
				modified = true;
				if(wezel&&wezel->last) wezel=wezel->last;
			    break;
			}
	}

}

System::Drawing::Point getXboundaries(Node* tree)
{
	System::Drawing::Point tmpRet=System::Drawing::Point(tree->getX(),tree->getX()+tree->getWidth()),tmptmp=tmpRet;
	if (tree->first)
		for (Node* tmp=tree->first;tmp;tmp=tmp->next)
		{
			tmptmp=getXboundaries(tmp);
			if (tmptmp.X<tmpRet.X)
				tmpRet.X=tmptmp.X;
			if (tmptmp.Y>tmpRet.Y)
				tmpRet.Y=tmptmp.Y;
		}
	return tmpRet;
}
System::Drawing::Point getYboundaries(Node* tree)
{
	System::Drawing::Point tmpRet=System::Drawing::Point(tree->getY(),tree->getY()+tree->getHeight()),tmptmp=tmpRet;
	if (tree->first)
		for (Node* tmp=tree->first;tmp;tmp=tmp->next)
		{
			tmptmp=getYboundaries(tmp);
			if (tmptmp.X<tmpRet.X)
				tmpRet.X=tmptmp.X;
			if (tmptmp.Y>tmpRet.Y)
				tmpRet.Y=tmptmp.Y;
		}
	return tmpRet;
}



void Keylistner::calculateDisplacementBoundaries()
{
	System::Drawing::Point tmpX,tmpY;
	tmpX=getXboundaries(this->getTree());
	this->minPosH=tmpX.X-150;
	this->maxPosH=tmpX.Y+150;
	tmpY=getYboundaries(this->getTree());
	this->minPosV=tmpY.X-150;
	this->maxPosV=tmpY.Y+150;

	
}